import { useState } from 'react';

/**
 * rowSelectionOption包含配置项：
 * rowClickSelectable 控制是否可以点击选择
 * customMaintain 直接使用外部传进来的rowSelection
 * isCrossPageSelect 是否支持跨页选取
 * 
*/

function useRowSelection(rowSelection) {
    const [ selectedRowKeys, setSelectedRowKeys ] = useState([]);
    let _rowSelection;
    if(!rowSelection) {
        _rowSelection = false;
    } else {
        const onSelectChange = (newSelectedRowKeys, selectedRows) => {
            if(typeof rowSelection.onChange === 'function') {
                rowSelection.onChange(newSelectedRowKeys, selectedRows);
            }
            setSelectedRowKeys(newSelectedRowKeys);
        };
    
        _rowSelection = {
            ...rowSelection,
            selectedRowKeys, 
            onChange: onSelectChange,
        };
    }

    return {
        _rowSelection
    }

}

export default useRowSelection;
